Private Sub mnuSortingGroupSort_Click()
Dim result%, jobnum%, mainjob%, fieldcount%, SortFieldN%, fieldHandle&, fieldLength%, Direction%
Dim FieldName$
result% = PEOpenEngine()
If result% = 0 Then
MsgBox "Could not start the report engine. Execution must halt.", vbOKOnly + vbCritical, "Serious Error"
End
End If
jobnum% = PEOpenPrintJob(lblReportName.Caption) ' Name from label on sample form
ErrorTrap "OpenPrintJob in SortingGroupSort", jobnum%
' Subreport check - if a subreport is currently selected on the main form, jobnum% becomes the subreport
If lblSubreportName.Visible Then
mainjob% = jobnum%
jobnum% = PEOpenSubreport(mainjob%, lblSubreportName.Caption)
ErrorTrap "OpenSubReport in SortingGroupSort", mainjob%
End If
' Count how many group sort fields there are
fieldcount% = PEGetNGroupSortFields(jobnum%)
If fieldcount% =-1 Then
' Something went wrong
ErrorTrap "GetNGroupSortFields in SortingGroupSort", jobnum%
ElseIf fieldcount% = 0 Then
' None!
MsgBox "There are no group sort fields defined in this report.", vbOKOnly + vbCritical, "No Group Sort Fields"
Else
' There are Group Sort fields
' The sort order display form is loaded so that the list box can be filled with sort fields
Load SortOrder
CenterForm Sample, SortOrder
SortOrder.Caption = "Group Sort Order"
' Loop through the number of group sort fields determined by PEGetNGroupSortFields
For SortFieldN% = 0 To fieldcount% - 1
' Get the fieldhandle, length and sort direction
result% = PEGetNthSortField(jobnum%, SortFieldN%, fieldHandle&, fieldLength%, Direction%)
ErrorTrap "GetNthSortField in SortingGroupSort", jobnum%
' Set chr$(0) into the fieldname string based on the length retrieved in the preceding call
' NOTE! Failure to do this step results in GPFs
FieldName$ = String$(fieldLength%, 0)
' crvbHandleToBstr fills the fieldname string with the name of the sort field
result% = crvbHandleToBstr(fieldHandle&, FieldName$, fieldLength%)
ErrorTrap "HandleToBstr for FieldName in SortingGroupSort", jobnum%
FieldName$ = Trim$(FieldName$)
FieldName$ = Left$(FieldName$, Len(FieldName$) - 1)
If Direction% = PE_SF_ASCENDING Then
FieldName$ = FieldName$ & " (A)"
ElseIf Direction% = PE_SF_DESCENDING Then
FieldName$ = FieldName$ & " (D)"
Else
MsgBox "The direction flag has been set to an impossible value. Execution must stop.", vbOKOnly + vbCritical, "Fatal Error"
End
End If
' The fieldname string is then inserted into the list box on the sort order form
SortOrder!lstSortList.AddItem FieldName$
Next SortFieldN%
' Default to first item on list
SortOrder!lstSortList.ListIndex = 0
' Once the list is complete, the sort order form is shown 1 ' Modally - execution in this module will stop
' until the sort order form is hidden (by pressing Ok or Cancel)
SortOrder.Show 1 ' Modal
' Now that execution has returned, check what button was pressed and react accordingly
Select Case SortOrder.Tag
Case "Ok"
If MsgBox("Do you want to change the group sort order of this print job?", vbYesNo + vbQuestion, "Change Group Sort Order?") = vbYes Then
' Make changes to the print job group sort order - note that this has no permanent affect on the report
' Changes made at the API level cannot be stored in the report
For SortFieldN% = 0 To SortOrder!lstSortList.ListCount - 1
' Pull the direction of the group sort field item first and store it
If Right$(SortOrder!lstSortList.List(SortFieldN%), 3) = "(A)" Then
Direction% = PE_SF_ASCENDING
Else
Direction% = PE_SF_DESCENDING
End If
' Copy the name of the group sort field from the list box
FieldName$ = SortOrder!lstSortList.List(SortFieldN%)
' Clip the direction off
FieldName$ = Left$(FieldName$, Len(FieldName$) - 4) & Chr$(0)
' Set the group sort field
result% = PESetNthGroupSortField(jobnum%, SortFieldN%, FieldName$, Direction%)
ErrorTrap "SetNthGroupSortField in SortingGroupSort", jobnum%
Next SortFieldN%
End If
Case "Cancel"
MsgBox "Cancel was pressed on the Sort Order form. The group sort order of the print job will not be changed.", vbOKOnly + vbInformation, "Cancel Pressed"
End Select
' Finished with the sort order form - unload it
Unload SortOrder
' Offer opportunity to see what you did to the report
If MsgBox("Do you want to preview the report?", vbYesNo + vbQuestion, "Preview Report?") = vbYes Then
' Simplified version of the custom-l ink preview routine (no custom buttons)
result% = PEOutputToWindow(jobnum%, "Group Sort Order Demonstration Preview" & Chr$(0), CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0)
ErrorTrap "OutputtoWindow in SortingGroupSort", jobnum%
result% = PEStartPrintJob(jobnum%, True)
ErrorTrap "StartPrintJob in SortingGroupSort", jobnum%
result% = 1
Do While result% <> 0
DoEvents
DoEvents
result% = PEGetWindowHandle(jobnum%)
Loop
End If
End If
' Close print job and engine
' Subreport check - if a subreport is currently open, call PreviewReport to offer a chance to
' preview the main report, then close the subreport and main report
If lblSubreportName.Visible Then
PreviewReport mainjob%
result% = PECloseSubreport(jobnum%)
ErrorTrap "CloseSubReport in SortingGroupSort", mainjob%
PEClosePrintJob mainjob%
Else
PEClosePrintJob jobnum%
End If
PECloseEngine
MsgBox "Sort Groups Complete!", vbOKOnly, "Operation Succeeded"
End Sub
ActiveX
Private Sub mnuSortingGroupSort_Click()
Dim SortFieldN As Integer
Dim FieldName As String, SortDirection As String
Dim hwndPreviewWindow As Long
CrystalReport1.ReportFileName = lblReportName.Caption ' Name from label on sample form
' Set up endless loop (only ends with an Exit Do) for editing multiple sort fields
Do While True
FieldName = InputBox("Enter group sort field to edit. Press Cancel to end editing of group sort fields:", "Enter Group Sort Field Number", "0")
' if a zero length string, then Cancel was pressed, exit the loop
If FieldName = "" Then Exit Do
' Otherwise a sort field number was entered, convert it
SortFieldN = Val(FieldName)
' Get FieldName
FieldName = InputBox("Enter field name(s) for the group sort field. Press Cancel to end editing of group sort fields. Field names must be encased in braces:", "Enter Group Sort Field Name:", "{}")
' if a zero length string, then Cancel was pressed, exit the loop
If FieldName = "" Then Exit Do
' Get Sort Direction
SortDirection = InputBox("Enter the sort direction. Press Cancel to end editing of group sort fields. Press 'A' for Ascending, 'D' for Descending:", "Enter Sort Direction", "A")
If SortDirection = "" Then Exit Do
If SortDirection = "A" Then SortDirection = "+" Else SortDirection = "-"
' Load sort field with data input
CrystalReport1.GroupSortFields(SortFieldN) = SortDirection & FieldName
Loop
' Offer opportunity to see what you did to the report
If MsgBox("Do you want to preview the report?", vbYesNo + vbQuestion, "Preview Report?") = vbYes Then
CrystalReport1.Destination = 0 ' Window
CrystalReport1.Action = 1 ' Print
ErrorTrap "SortingGroupSort"
hwndPreviewWindow = GetActiveWindow()
Do While IsWindow(hwndPreviewWindow)
DoEvents
Loop
End If
' Close the report
CrystalReport1.ReportFileName = ""
MsgBox "Sort Groups Complete!", vbOKOnly, "Operation Completed"
End Sub
Seagate Software IMG Holdings, Inc. http://www.seagatesoftware.com Support services: http://support.seagatesoftware.com |